package com.bnyy.medicalHousekeeper.moudle.health.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.bnyy.common.bean.UserInfo;
import com.bnyy.gbp.customsView.ScaleImageView;
import com.bnyy.health.bean.BloodPressureWeekly;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.moudle.health.fragment.BloodPressureWeeklyFragment;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.function.Consumer;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author : 关炳鹏
 * Description : 血压健康周报
 * Date : 2022/8/29
 **/
public class BloodPressureWeeklyShareActivity extends BaseActivityImpl implements BloodPressureWeeklyFragment.Callback {

    @BindView(R.id.iv_header)
    ScaleImageView ivHeader;
    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.fl_container)
    FrameLayout flContainer;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.rl_root)
    RelativeLayout rlRoot;
    @BindView(R.id.ns_root)
    NestedScrollView nsRoot;
    @BindView(R.id.tv_share_to_wx)
    TextView tvShareToWx;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;

    public static void show(Context context, BloodPressureWeekly bloodPressureWeekly, UserInfo userInfo) {
        Intent intent = new Intent(context, BloodPressureWeeklyShareActivity.class);
        intent.putExtra("bloodPressureWeekly", bloodPressureWeekly);
        intent.putExtra("userInfo", userInfo);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_health_blood_pressure_weekly_share;
    }

    @Override
    public String getTitleStr() {
        return null;
    }

    @Override
    public boolean unuseRefreshLayout() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();


        UserInfo userInfo = (UserInfo) intent.getSerializableExtra("userInfo");
        tvUsername.setText(userInfo.getUsername());
        GlideHelper.setCircleImage(mContext, userInfo.getUserimg(), ivHeader, 1, Color.WHITE, R.mipmap.icon_default_header_man);

        BloodPressureWeekly bloodPressureWeekly = (BloodPressureWeekly) intent.getSerializableExtra("bloodPressureWeekly");

        if (bloodPressureWeekly != null) {
            String startDatetime = bloodPressureWeekly.getStart_datetime();
            String endDatetime = bloodPressureWeekly.getEnd_datetime();
            SimpleDateFormat sdfYMDHMS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Calendar calendar = Calendar.getInstance();

                calendar.setTime(sdfYMDHMS.parse(startDatetime));
                int fromYear = calendar.get(Calendar.YEAR);
                int fromMonth = calendar.get(Calendar.MONTH) + 1;
                int fromDay = calendar.get(Calendar.DAY_OF_MONTH);

                calendar.setTime(sdfYMDHMS.parse(endDatetime));
                int toYear = calendar.get(Calendar.YEAR);
                int toMonth = calendar.get(Calendar.MONTH) + 1;
                int toDay = calendar.get(Calendar.DAY_OF_MONTH);

                StringBuilder sb = new StringBuilder();
                sb.append(fromYear)
                        .append("年")
                        .append(fromMonth)
                        .append("月")
                        .append(fromDay)
                        .append("日")
                        .append("至");

                if (fromYear == toYear) {
                    if (fromMonth != toMonth) {
                        sb.append(toMonth).append("月");
                    }
                    sb.append(toDay).append("日");
                } else {
                    sb.append(toYear)
                            .append("年")
                            .append(toMonth)
                            .append("月")
                            .append(toDay)
                            .append("日");
                }
                tvDate.setText(sb.toString());
            } catch (ParseException e) {
                e.printStackTrace();
            }

            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(flContainer.getId(), BloodPressureWeeklyFragment.getInstance(bloodPressureWeekly, true));
            transaction.commitNowAllowingStateLoss();
        }

    }

    @Override
    public void onUpdate(Consumer<BloodPressureWeekly> consumer) {

    }

    @OnClick({
            R.id.tv_share_to_wx,
            R.id.tv_cancel
    })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_share_to_wx:
                int h = 0;
                Bitmap bitmap;
                for (int i = 0; i < nsRoot.getChildCount(); i++) {
                    h += nsRoot.getChildAt(i).getHeight();
                }
                bitmap = Bitmap.createBitmap(nsRoot.getWidth(), h, Bitmap.Config.RGB_565);
                Canvas canvas = new Canvas(bitmap);

                nsRoot.draw(canvas);

//                //初始化 WXImageObject 和 WXMediaMessage 对象
//                WXImageObject imgObj = new WXImageObject(bitmap);
//                WXMediaMessage msg = new WXMediaMessage();
//                msg.mediaObject = imgObj;
//
//                //设置缩略图
//                Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, 160, 320, true);
//                bitmap.recycle();
//
//                ByteArrayOutputStream baos = new ByteArrayOutputStream();
//                thumbBmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
//                thumbBmp.recycle();
//                msg.thumbData = baos.toByteArray();
//                //构造一个Req
//                SendMessageToWX.Req req = new SendMessageToWX.Req();
//                req.transaction = "img_" + System.currentTimeMillis();
//                req.message = msg;
//                req.scene = SendMessageToWX.Req.WXSceneSession;
////                        req.userOpenId = Constant.WX.APP_ID;
//                //调用api接口，发送数据到微信
//                IWXAPI api = WXAPIFactory.createWXAPI(mContext, Constant.WX.APP_ID);
//                api.sendReq(req);
                break;
            case R.id.tv_cancel:
                break;
        }
        finish();
    }
}



































